<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\CustomerModel;
use cmf\controller\AdminBaseController;
use think\Db;

class CustomerController extends AdminBaseController
{

    public function initialize()
    {
        $this->deal_model=Db::name("customer");
        $this->user_model=Db::name("user");
        parent::initialize();
    }

    /**
     * 客户管理
     */
	public function index(){
        $data = $this->request->param('customer_name');
        $data1 = $this->request->param('customer_phone');
        $results = $this->deal_model->alias('c')->field('u.user_nickname,c.*')
            ->join('dj_user u','u.id=c.add_user')
            ->where('customer_name','like','%'.$data.'%')
            ->where('customer_phone','like','%'.$data1.'%')
            ->order('c.id', 'DESC')->paginate(15);

        $page = $results->render();
        $this->assign("results",$results);
        $this->assign("page", $page);

        return $this->fetch();
	}

	/*跳转添加*/
	public function add(){

        $result = Db::name("user")->alias('u')->field('u.user_nickname')
            ->join('dj_customer c','c.add_user=u.id')
            ->where('u.id',cmf_get_current_admin_id())
            ->find();

        //echo Db::name("user")->getLastSql();
        $name = cmf_get_admin_name();
        $this->assign($result);
        $this->assign("name",$name);
        return $this->fetch();
	}

	/*添加保存*/
	public function add_post(){

        $data           = $this->request->param();
        $data['add_time'] =time();
        $data['add_user'] =cmf_get_current_admin_id();
        //dump($data);exit;

        $this->deal_model->insert($data);
        $this->success("添加成功！", url("customer/index"));
    }

    /*跳转编辑*/
    public function edit(){

        $id = $this->request->param("id",0,'');
        $where = array(
            'id'	=> $id
        );
        $result = $this->deal_model->where($where)->find();

        $this->assign($result);

        return $this->fetch();
    }

    /*编辑保存*/
    public function edit_post(){

        $data = $this->request->param();
        $data['add_time'] = time();
        $data['add_user'] = cmf_get_current_admin_id();

        $this->deal_model->update($data);
        $this->success("保存成功",url("customer/index"));
    }

    /*删除*/
    public function delete(){

        $data = $this->request->param();

        $this->deal_model->delete($data);
        $this->success("删除成功",url("customer/index"));
    }

}